﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;

namespace TaxiDispatchClient
{
    public partial class TaxiNoPsgConfigSetForm : DevExpress.XtraEditors.XtraForm
    {
        public TaxiNoPsgConfigSetForm()
        {
            InitializeComponent();
            BindConfig();
            lb_title.Text = "所有车辆默认参数";
        }

        private string _phoneNumber = "";

        private Model.TaxiNoPsgDistanceConfig _model;


        public TaxiNoPsgConfigSetForm(string phoneNumber)
        {
            InitializeComponent();
            lb_title.Text = phoneNumber;
            _phoneNumber = phoneNumber;
            try
            {
                _model = MainForm.PositionService.TaxiNoPsgConfigGetModel(phoneNumber);
            }
            catch (Exception ex)
            {
                Common.CommonRoutine.HandlingExceptions(ex);                
            }                       
        }

        private void TaxiParameterSetForm_Load(object sender, EventArgs e)
        {
            BindConfigByTaix();
        }

        private void BindConfigByTaix()
        {
            if (_model != null)
            {
                timeEdit_s1.Time = _model.StartTime1;
                timeEdit_e1.Time = _model.EndTime1;
                timeEdit_s2.Time = _model.StartTime2;
                timeEdit_e2.Time = _model.EndTime2;
                textEdit_d1.Text = _model.Distance1.ToString();
                textEdit_d2.Text = _model.Distance2.ToString();
            }
            //else
            //{
            //    BindConfig();
            //}
        }

        private void BindConfig()
        {
            //string[] config = Common.OperateConfig.GetClientTaxiWarmingConfig();
            //textEdit_speed.Text = config[0];
           
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if(btn_save.Tag.ToString()=="保存")
            {    
                if(!CheckPassed()) return;
                //// _taxiId 为-1时 设置出租车报警配置文件  不为 -1 时对单辆出租车设置
                //if (_taxiId == -1)
                //{
                //    string[] config = new string[6];
                //    config[0] = textEdit_speed.Text;
                //    config[1] = checkEdit_speed.Checked ? "1" : "0";
                //    config[2] = textEdit_stopTimeOut.Text;
                //    config[3] = checkEdit_stopTimeOut.Checked ? "1" : "0";
                //    config[4] = textEdit_power.Text;
                //    config[5] = checkEdit_power.Checked ? "1" : "0";
                //    Common.OperateConfig.SaveClientTaxiWarmingConfig(config);
                //    XtraMessageBox.Show("修改成功！");
                //}
                //else
                //{
                   //if(_model!=null)
                   //{
                TaxiNoPsgDistanceConfig model = new TaxiNoPsgDistanceConfig();
                       model.PhoneNumber = _phoneNumber;
                       model.StartTime1 = timeEdit_s1.Time;
                       model.EndTime1 = timeEdit_e1.Time;
                       model.Distance1 = int.Parse(textEdit_d1.Text);
                       model.StartTime2 = timeEdit_s2.Time;
                       model.EndTime2 = timeEdit_e2.Time;
                       model.Distance2 = int.Parse(textEdit_d2.Text);
                  // }
                   if(MainForm.PositionService.TaxiNoPsgConfigAdd(model))
                   {
                       XtraMessageBox.Show("修改成功！");
                   }
                   else
                   {
                       XtraMessageBox.Show("修改失败，可能是未找到该车辆信息或服务器通讯故障");
                   }
                //}
                ResetControl(false);
                btn_save.Tag = "修改";
                btn_save.Image = Properties.Resources.UserForm_btnmodify001;
                btn_cancel.Visible = false;
            }
            else
            {
                ResetControl(true);
                btn_save.Tag = "保存";
                btn_save.Image = Properties.Resources.UserForm_btnsave001;
                btn_cancel.Visible = true;
            }
        }

        private void ResetControl(bool enable)
        {
            timeEdit_s1.Enabled = enable;
            timeEdit_e1.Enabled = enable;
            timeEdit_s2.Enabled = enable;
            timeEdit_e2.Enabled = enable;
            textEdit_d1.Enabled = enable;
            textEdit_d2.Enabled = enable;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            btn_cancel.Visible = false;
            ResetControl(false);
            btn_save.Tag = "修改";
            btn_save.Image = Properties.Resources.UserForm_btnmodify001;
            BindConfigByTaix();
        }


        private bool CheckPassed()
        {
           if(timeEdit_s1.Text.Trim()=="")
           {
               XtraMessageBox.Show("时间段1开始时间不能为空！");
               timeEdit_s1.Focus();
               return false;
           }
           if (timeEdit_e1.Text.Trim() == "")
           {
               XtraMessageBox.Show("时间段1结束时间不能为空！");
               timeEdit_e1.Focus();
               return false;
           }
           if (textEdit_d1.Text.Trim()==""||textEdit_d1.Text.Trim() == "0")
           {
               XtraMessageBox.Show("时间段1最大空载里程不能为空且需大于0！");
               textEdit_d1.Focus();
               return false;
           }    
           if (timeEdit_s2.Text.Trim() == "")
           {
               XtraMessageBox.Show("时间段2开始时间不能为空！");
               timeEdit_s2.Focus();
               return false;
           }
           if (timeEdit_e2.Text.Trim() == "")
           {
               XtraMessageBox.Show("时间段2结束时间不能为空！");
               timeEdit_e2.Focus();
               return false;
           }
           if (textEdit_d2.Text.Trim()==""||textEdit_d2.Text.Trim() == "0")
           {
               XtraMessageBox.Show("时间段2最大空载里程不能为空且需大于0！");
               textEdit_d2.Focus();
               return false;
           }
 
           return true;
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion



    }
}